home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / www / library / implemen / htatom.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  2.8 KB  |  117 lines

  1. /*            Atoms: Names to numbers            HTAtom.c
  2. **            =======================
  3. **
  4. **    Atoms are names which are given representative pointer values
  5. **    so that they can be stored more efficiently, and comparisons
  6. **    for equality done more efficiently.
  7. **
  8. **    Atoms are kept in a hash table consisting of an array of linked lists.
  9. **
  10. ** Authors:
  11. **    TBL    Tim Berners-Lee, WorldWideWeb project, CERN
  12. **    (c) Copyright CERN 1991 - See Copyright.html
  13. **
  14. */
  15. #define HASH_SIZE    101        /* Tunable */
  16. #include"capalloc.h"
  17. #include "HTAtom.h"
  18.  
  19. #include"capstdio.h"
  20. #include <stdio.h>                /* joe@athena, TBL 921019 */
  21. #include <string.h>
  22.  
  23. #include "HTUtils.h"
  24. #include "HTList.h"
  25. PRIVATE HTAtom * hash_table[HASH_SIZE];
  26. PRIVATE BOOL initialised = NO;
  27.  
  28. PUBLIC HTAtom * HTAtom_for ARGS1(CONST char *, string)
  29. {
  30.     int hash;
  31.     CONST char * p;
  32.     HTAtom * a;
  33.  
  34.     /*        First time around, clear hash table
  35.     */
  36.     if (!initialised) {
  37.         int i;
  38.     for (i=0; i<HASH_SIZE; i++)
  39.         hash_table[i] = (HTAtom *) 0;
  40.     initialised = YES;
  41.     }
  42.     
  43.     /*        Generate hash function
  44.     */
  45.     for(p=string, hash=0; *p; p++) {
  46.         hash = (hash * 3 + *p) % HASH_SIZE;
  47.     }
  48.     
  49.     /*        Search for the string in the list
  50.     */
  51.     for (a=hash_table[hash]; a; a=a->next) {
  52.     if (0==strcmp(a->name, string)) {
  53.             /* if (TRACE) fprintf(stderr,
  54.             "HTAtom: Old atom %p for `%s'\n", a, string); */
  55.         return a;                /* Found: return it */
  56.     }
  57.     }
  58.     
  59.     /*        Generate a new entry
  60.     */
  61.     a = (HTAtom *)malloc(sizeof(*a));
  62.     if (a == NULL) outofmem(__FILE__, "HTAtom_for");
  63.     a->name = (char *)malloc(strlen(string)+1);
  64.     if (a->name == NULL) outofmem(__FILE__, "HTAtom_for");
  65.     strcpy(a->name, string);
  66.     a->next = hash_table[hash];        /* Put onto the head of list */
  67.     hash_table[hash] = a;
  68. /*    if (TRACE) fprintf(stderr, "HTAtom: New atom %p for `%s'\n", a, string); */
  69.     return a;
  70. }
  71.  
  72.  
  73. PRIVATE BOOL mime_match ARGS2(CONST char *, name,
  74.                   CONST char *, templ)
  75. {
  76.     if (name && templ) {
  77.     static char *n1 = NULL;
  78.     static char *t1 = NULL;
  79.     char *n2;
  80.     char *t2;
  81.  
  82.     StrAllocCopy(n1, name);        /* These also free the ones    */
  83.     StrAllocCopy(t1, templ);    /* from previous call.        */
  84.  
  85.     if (!(n2 = strchr(n1, '/'))  ||  !(t2 = strchr(t1, '/')))
  86.         return NO;
  87.  
  88.     *(n2++) = (char)0;
  89.     *(t2++) = (char)0;
  90.  
  91.     if ((0==strcmp(t1, "*") || 0==strcmp(t1, n1)) &&
  92.         (0==strcmp(t2, "*") || 0==strcmp(t2, n2)))
  93.         return YES;
  94.     }
  95.     return NO;
  96. }
  97.     
  98.  
  99. PUBLIC HTList *HTAtom_templateMatches ARGS1(CONST char *, templ)
  100. {
  101.     HTList *matches = HTList_new();
  102.  
  103.     if (initialised && templ) {
  104.     int i;
  105.     HTAtom *cur;
  106.  
  107.     for (i=0; i<HASH_SIZE; i++) {
  108.         for (cur = hash_table[i];  cur;  cur=cur->next) {
  109.         if (mime_match(cur->name, templ))
  110.             HTList_addObject(matches, (void*)cur);
  111.         }
  112.     }
  113.     }
  114.     return matches;
  115. }
  116.  
  117.